# TODO: Add comment
# 
# Author: rogb
###############################################################################

cashNumberToText <- function(Amount){
	n <- length(Amount)
	out <- rep(paste(rep(letters,3),collapse=""),n)
	.C("cashNumberToText",as.double(Amount),as.integer(n),out)[[3]]
}

if(F){
cashNumberToText <- function(Amount){
	# Amount <- 1000000000
	AmountSign <- sign(Amount)
	Amount <- round(Amount * AmountSign,2)
	RAmount <- Amount - floor(Amount) 
	Amount <- floor(Amount)
	textPieces <- c()
	
	while(Amount>0){
		nT <- Amount / 1000
		stepA <- Amount - floor(nT)*1000
		stepP <- as.character(round(stepA,2))
		if(nchar(stepP)<3 & (Amount-stepA)/1000>0){
			stepP <- paste(c(rep("0",3-nchar(stepP)),stepP),collapse="")
		}
		textPieces <- c(stepP,textPieces)
		Amount <- (Amount-stepA)/1000
	}
	
	roundText <- as.character(round(RAmount,2))
	if(nchar(roundText)==1){
		roundText <- paste(roundText,".",sep="")
	}
	roundText <- substring(roundText,2)
	Result <- paste(textPieces,collapse="'")
	while(nchar(roundText)<3){
		roundText <- paste(roundText,"0",sep="")
	}
	
	Result <- paste(Result,roundText,sep="")

	if(AmountSign<0){
		Result <- paste("-",Result,sep="")
	}
	Result
}
}

if(F){
x <- rnorm(100000) * 70000000000

now <- proc.time()
out1 <- cashNumberToText(x)
print(proc.time()-now)


now <- proc.time()
out2 <- sapply(x,cashNumberToText)
print(proc.time()-now)
}


